package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.SysUser;
import com.gec.model.vo.SysUserQueryVo;

import com.gec.system.aop.SystemControllerLog;
import com.gec.system.service.SysUserService;
import com.gec.system.common.Result;
import com.gec.system.util.MD5Helper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author dafei
 * @since 2024-05-22
 */
@RestController
@Api(tags = "用户管理控制器")
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    //分页和条件查询
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation("分页和条件查询")
    @GetMapping("/{page}/{limit}")
    public Result selectUserPageByVo(@PathVariable long page,
                                     @PathVariable Long limit,
                                    SysUserQueryVo sysUserQueryVo)
    {
        IPage<SysUser> iPage=new Page<>(page,limit);
        iPage=this.sysUserService.selectPage(iPage,sysUserQueryVo);
        return Result.ok(iPage);
    }

//    //添加用户
//    @PostMapping("/addUser")
//    @ApiOperation("添加用户")
//    public  Result addUser(@RequestBody SysUser sysUser){
//        boolean b=this.sysUserService.save(sysUser);
//        if(b){return Result.ok();}
//        else return Result.fail();
//    }

    //添加用户
    @SystemControllerLog(operate = "INSERT",module = "用户管理")
    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @PostMapping("/addUser")
    @ApiOperation("添加用户")
    public  Result addUser(@RequestBody SysUser sysUser){
       //通过md5工具类
        String passwordwithMD5= MD5Helper.encrypt(sysUser.getPassword());
        //将加密密码设置给sysuser
        sysUser.setPassword(passwordwithMD5);

        boolean b=this.sysUserService.save(sysUser);
        if(b){return Result.ok();}
        else return Result.fail();
    }



//     @GetMapping("/userListPage")
// @SystemControllerLog(operate = "用户查询",module = "用户管理")
// public Result findUserList( @RequestParam Integer pageNum,
//                                @RequestParam Integer pageSize,
//                                @RequestParam String username,
//                                @RequestParam String loveValue,
//                                @RequestParam String address) {}
//  @PostMapping("/addOrUpdate")
//  @SystemControllerLog(operate = "用户修改或者添加",module = "用户管理")
//  public Result addOrUpdateUser(@RequestBody User user){}


    // 根据id 去获取一个用户

    @GetMapping("findUserById/{id}")
    @ApiOperation("根据id查询用户")
    public Result findUserById(@PathVariable Long id)
    {
        SysUser sysUser = this.sysUserService.getById(id);
        return Result.ok(sysUser);
    }

    // 修改
    @SystemControllerLog(operate = "UPDATE",module = "用户管理")
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation("修改用户")
    @PostMapping("updateUser")
    public Result
    updateUser(@RequestBody SysUser sysUser)
    {
        boolean isSuccess = this.sysUserService.updateById(sysUser);
        if (isSuccess)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }

    //删除单个
    @SystemControllerLog(operate = "REMOVE",module = "用户管理")
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @DeleteMapping("removeUserById/{id}")
    @ApiOperation("根据id删除")
    public Result removeUserById(@PathVariable Long id)
    {
        boolean isSuccess = this.sysUserService.removeById(id);
        if (isSuccess) { return Result.ok(); }
        else { return Result.fail(); }
    }


    // 更改用户状态
    @SystemControllerLog(operate = "UPDATE",module = "用户管理")
    @ApiOperation("更改用户状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,
                               @PathVariable Integer status)
    {
        this.sysUserService.updateStatus(id,status);
        return Result.ok();
    }


}

